//
//  ReadEssayViewController.m
//  YHYRealx
//
//  Created by yanghuiyan on 16/4/25.
//  Copyright © 2016年 yanghuiyan. All rights reserved.
//

#import "ReadEssayViewController.h"
#import "DetailEssayModel.h"
#import "ReadEssayDetailTableViewCell.h"
#import "ReadEssayCommentTableViewCell.h"
@interface ReadEssayViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain) DetailEssayModel *detailEssayModel;

@property (nonatomic, retain) AutoModel *autoModel;

@property (nonatomic, retain) NSMutableArray *dataSource;

@property (nonatomic, retain) NSMutableArray *arraySame;

@property (nonatomic, retain) YHYBaseTableView *tableView;
@end

@implementation ReadEssayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    /**
     *
     */
//    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
//    UITableView *tableView = [UITableView new];
//    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    /**
     *
     */
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self creatView];
    self.dataSource = [NSMutableArray array];
    [self dataHandle];
}



#pragma mark - 数据解析
- (void) dataHandle {
    
    NSString *string = [NSString stringWithFormat:@"http://v3.wufazhuce.com:8000/api/essay/%@", self.idNew];
    
    [YHYAFNetTool GETNetWirhUrl:string body:nil headerFile:nil response:YHYJSON Success:^(id result) {
        
        self.detailEssayModel = [[DetailEssayModel alloc] initWithDataSource:result[@"data"]];
        [self.dataSource addObject: self.detailEssayModel];
        
        NSArray *arr = result[@"data"][@"author"];
        for (NSDictionary *dic in arr) {
            self.autoModel = [[AutoModel alloc] initWithDataSource:dic];
        }
        
        [_detailEssayModel release];
        [self.tableView reloadData];
        
    } Failure:^(NSError *error) {
        NSLog(@"failure");
    }];
    
}


#pragma mark - 创建视图
- (void)creatView {
    
    self.view.backgroundColor = [UIColor colorWithRed:247 / 255.0 green:248 / 255.0 blue:249 / 255.0 alpha:1];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
    titleLabel.text = @"短篇";
    titleLabel.font = [UIFont boldSystemFontOfSize:22];
    titleLabel.textColor = [UIColor grayColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLabel;
    [titleLabel release];
    
    
    self.tableView = [[YHYBaseTableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) style:UITableViewStylePlain];
    [self.tableView registerClass:[ReadEssayDetailTableViewCell class] forCellReuseIdentifier:@"detail"];
//    [self.tableView registerClass:[ReadEssayCommentTableViewCell class] forCellReuseIdentifier:@"Comment"];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_tableView];
    
    [_tableView release];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

//- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
//    if (section == 0) {
//        return nil;
//    }
//    return @"相关推荐";
//}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

           if (_dataSource.count != 0) {
            
            return _dataSource.count;
        }
       return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
        if (_dataSource.count != 0) {
            
            ReadEssayDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"detail"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.essayModel = self.detailEssayModel;
            cell.autoModel = self.autoModel;
            
            return cell;
        }
    
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
   if (_dataSource.count != 0) {
                
        DetailEssayModel *model = self.dataSource[indexPath.row];
                
        CGFloat Height = [ReadEssayDetailTableViewCell heightWithTitle:model.hp_title];
                
        return HEIGHT * 0.7 + 120 + Height;
   }
    return 0;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
